<?php
$eigene_gstelle = $_SESSION['gstelleId'];

if( isset($_GET['protokoll-id']) ){
    $kundenNr = $_GET['kunden-nr'];
    $contactId = $_GET['protokoll-id'];
    $query = "SELECT
                `idKontaktaufnahme`,
                `kontaktDurch`,
                `nachricht`,
                `Kunde`.`nachname` AS `k_nachname`,
                `Kunde`.`vorname` AS `k_vorname`,
                `Mitarbeiter`.`vorname` AS `m_vorname`,
                `Mitarbeiter`.`name` AS `m_nachname`
                FROM `kssystem`.`kontaktaufnahme`, `kssystem`.`Kunde`, `kssystem`.`Mitarbeiter` WHERE `kundeId`=`idKunde` AND `kontaktaufnahme`.`bearbeiterId`=`Mitarbeiter`.`idMitarbeiter` AND `idkontaktaufnahme`='$contactId' LIMIT 1";
    $stm = $sql->prepare($query);
    $stm->execute();
    $contacts = $stm->fetchAll(PDO::FETCH_ASSOC);
?>
<div><b>Protokoll ID:</b> <?= $contacts[0]['idKontaktaufnahme'] ?></div>
<div><b>Kontaktaufnahme durch:</b> <?= ucfirst($contacts[0]['kontaktDurch']) ?></div>
<div><b>Bearbeiter:</b> <?= $contacts[0]['m_nachname'] ?>, <?= $contacts[0]['m_vorname'] ?></div>
<div><b>Protokoll Nachricht:</b></div>
<div><?= $contacts[0]['nachricht'] ?></div>
<br/>
<div><a href="?page=kontakthistorie&kunden-nr=<?= $_GET['kunden-nr'] ?>">&lt;&lt;zur&uuml;ck&lt;&lt;</a></div>



<?php
}elseif ( isset($_GET['kunden-nr']) ){
    $kundenNr = $_GET['kunden-nr'];
    $query = "SELECT * FROM `kssystem`.`kontaktaufnahme`, `kssystem`.`Kunde` WHERE `kundeId`=`idKunde` AND `idKunde`='$kundenNr'";
    $stm = $sql->prepare($query);
    $stm->execute();
    $contacts = $stm->fetchAll(PDO::FETCH_ASSOC);
    
    if ( is_array($contacts)){  ?>
    <script lang="JavaScript">
            $(document).ready(function() {
                $('#js_table_layout').dataTable();
            } );
    </script>
    <div class="clear"></div>
    <div>
            <table id="js_table_layout" style="width: 100%">
                    <thead>
                            <tr>
                                    <th style="width: 15%;text-align: left; text-indent: 5px;">Protokoll ID</th>
                                    <th style="width: 25%; text-align: left; text-indent: 5px;">Datum</th>
                                    <th style="width: 50%; text-align: left; text-indent: 5px;">Protokollnachricht</th>
                                    <th style="width: 10%; text-align: left; text-indent: 5px;">&nbsp;</th>
                            </tr>
                    </thead>
                    <tbody>
    <?php
    
    foreach ( $contacts as $contact ){
        if ($contact['datum'] != "") {
            $datum = date("d.m.Y" , strtotime($contact['datum']));
        }else{
            $datum = '-';
        }
    ?>
                            <tr>
                                    <td style="text-indent: 10px;"><?= $contact['idkontaktaufnahme'] ?></td>
                                    <td style="text-indent: 10px;"><?= $datum ?></td>
                                    <td style="text-indent: 10px;"><?= substr($contact['nachricht'], 0, 40) ?></td>
                                    <td style="text-align: center;">
                                        <a href="?page=kontakthistorie&kunden-nr=<?= $_GET['kunden-nr'] ?>&protokoll-id=<?= $contact['idkontaktaufnahme'] ?>">Details</a>
                                    </td>
                            </tr>
    <?php
    }
    ?>
                    </tbody>
            </table>
    </div>
<?php
    }
    
    
}else{
    $query = "SELECT * FROM `kssystem`.`kontaktaufnahme`, `kssystem`.`Kunde` WHERE `kundeId`=`idKunde` AND `gstelleId`='$eigene_gstelle' GROUP BY `kundenNr`";
    $stm = $sql->prepare($query);
    $stm->execute();
    $customers = $stm->fetchAll(PDO::FETCH_ASSOC);
    $param = "kontakthistorie";
    include_once('customers_table.php');
}



?>